using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Entities;
namespace DAL
{
   public class tbDiadanhDAL
    {
       private SqlConnection cn = null;
        public tbDiadanhDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(tbDiadanh dd)
        {
            SqlCommand cm = new SqlCommand("sp_diadanh_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@madiadiem", dd.Madiadiem);
            cm.Parameters.AddWithValue("@tendiadanh", dd.Tendiadanh);
            cm.Parameters.AddWithValue("@motadiadanh", dd.Motadiadanh);
            cm.Parameters.AddWithValue("@hinhdiadanh", dd.Hinhdiadanh);
            cm.ExecuteNonQuery();
        }
        public void Update(tbDiadanh dd)
        {
            SqlCommand cm = new SqlCommand("sp_diadanh_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@madiadanh", dd.Madiadanh);
            cm.Parameters.AddWithValue("@madiadiem", dd.Madiadiem);
            cm.Parameters.AddWithValue("@tendiadanh", dd.Tendiadanh);
            cm.Parameters.AddWithValue("@motadiadanh", dd.Motadiadanh);
            cm.Parameters.AddWithValue("@hinhdiadanh", dd.Hinhdiadanh);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("sp_diadanh_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@madiadanh", iD);
            cm.ExecuteNonQuery();
        }
        public tbDiadanhCollection Get(string where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_diadanh_Get_2000", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbDiadanhCollection ddColl = new tbDiadanhCollection();

            while (dr.Read()) 
            {
                tbDiadanh dd = new tbDiadanh();
                dd.Madiadanh = int.Parse(dr.GetValue(0).ToString());
                dd.Madiadiem = int.Parse(dr.GetValue(1).ToString());
                dd.Tendiadanh = dr.GetValue(2).ToString();
                dd.Motadiadanh = dr.GetValue(3).ToString();
                dd.Hinhdiadanh = dr.GetValue(4).ToString();
                ddColl.Add(dd);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return ddColl;
        }
    }
}
